package com.cskaoyan.config.realm;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketPermissionMapper;
import com.cskaoyan.mapper.MarketRoleMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName CustomRealm
 * @Description TODO
 * @Author by Lester
 * @Date 2022/11/21 22:03
 * @Version 1.0
 */
@Component
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    MarketAdminMapper mapper;

    @Autowired
    MarketUserMapper userMapper;




    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Autowired
    MarketPermissionMapper marketPermissionMapper;
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 通常根据AuthenticationToken中传入的用户名信息查询出该用户在数据库中的信息
        String type = ((MarketToken) authenticationToken).getType();
        String username = (String) authenticationToken.getPrincipal();

        /**
         * 张家齐
         */
        if ("admin".equals(type)) {
            MarketAdminExample example = new MarketAdminExample();
            example.createCriteria().andUsernameEqualTo(username);
            List<MarketAdmin> marketAdmins = mapper.selectByExample(example);
            if (marketAdmins.size() == 1) {
                //说明数据库中有一条对应的信息(用户名存在)
                MarketAdmin marketAdmin = marketAdmins.get(0);
                // 构造认证信息时，可以放入你需要的用户信息，而你放入的用户信息，可以作为Principals
                // 放入这个信息，是为了取出这个信息
                // 第二个参数credentials，是真实（正确）的密码，会和AuthenticationToken中的password做比较
                //这里的marketAdmin就是subject中的Principals。
                return new SimpleAuthenticationInfo(marketAdmin,marketAdmin.getPassword(),getName());
            }
        }
            /**
             * 李博
             */
         else if ("wx".equals(type)) {
            //查询user表中的信息
            MarketUserExample example = new MarketUserExample();
            example.createCriteria().andUsernameEqualTo(username);
            List<MarketUser> marketUsers = userMapper.selectByExample(example);
            if (marketUsers.size() == 1) {
                MarketUser marketUser = marketUsers.get(0);
                return new SimpleAuthenticationInfo(marketUser, marketUser.getPassword(), getName());
            }
        }
        return null;
    }

//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        // 要先获得Principal信息
//        MarketAdmin principal = (MarketAdmin) principalCollection.getPrimaryPrincipal();
//        // 根据用户信息查询出对应的权限列表
//        // mybatis来查询
//        List<String> permissions = Arrays.asList("aaa");
//        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        simpleAuthorizationInfo.addStringPermissions(permissions);
//        return simpleAuthorizationInfo;
//    }

    /**
     * 授权
     * @author zjq
     * @date 2022/11/22 18:52
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        // 要先获得Principal信息
        MarketAdmin principal = (MarketAdmin) principalCollection.getPrimaryPrincipal();
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        // 根据用户信息查询出对应的权限列表
        String username = principal.getUsername();
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);

        for (MarketAdmin marketAdmin : mapper.selectByExample(example)) {
            Integer[] roleIds = marketAdmin.getRoleIds();
            for (Integer roleId : roleIds) {
                MarketPermissionExample marketPermissionExample = new MarketPermissionExample();
                marketPermissionExample.createCriteria().andRoleIdEqualTo(roleId);
                List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(marketPermissionExample);
                for (MarketPermission marketPermission : marketPermissions) {
                    simpleAuthorizationInfo.addStringPermission(marketPermission.getPermission());
                }
            }
        }
//        List<String> permissions = Arrays.asList("aaa");
        //permissions  admin:admin:list 的形式
//        simpleAuthorizationInfo.addStringPermissions(permissions);
        return simpleAuthorizationInfo;
    }


}
